package com.lgx.mall.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.common.util.LayerOrderUtils;
import com.lgx.mall.core.dao.ICategoryDao;
import com.lgx.mall.core.entity.Category;
import com.lgx.mall.core.service.ICategoryService;

@Service("categoryService")
public class CategoryService implements ICategoryService {

	@Autowired
	private ICategoryDao categoryDao;
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	public ICategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public Category createCategory(Category category) throws DataException {
		if(category.getParent() != null)
		{
			String layerOrder = LayerOrderUtils.createLayerOrder("TM_CATEGORY", category.getParent().getId()+"", this.baseDao);
			category.setLayerOrder(layerOrder);
		}
		return categoryDao.createCategory(category);
	}

	public Category updateCategory(Category category) throws DataException {
		return categoryDao.updateCategory(category);
	}

	public void deleteCategory(Category category) throws DataException {
		categoryDao.deleteCategory(category);
	}

	public void deleteCategory(long id) throws DataException {
		//先删除子分类
		List<Category> ca =  categoryDao.findChildrenCategoryListById(id);
		if(ca.size() != 0){
			for(Category category : ca){
				this.deleteCategory(category.getId());
			}
		}
		//再删除父分类
		categoryDao.deleteCategory(id);
	}

	public Category findCategoryById(long id) {
		return categoryDao.findCategoryById(id);
	}

	public List<Category> findCategoryList(String sql) {
		return categoryDao.findCategoryList(sql);
	}

	public List<Category> findBrandCategoryList(long brandId) {
		return categoryDao.findBrandCategoryList(brandId);
	}

	public List<Category> findCategoryList() {
		List<Category> ca = categoryDao.findCategoryList();
		return ca;
	}
	
	public List<TreeModel> findCategoryListToCombotree() {
		List<TreeModel> ca = categoryDao.findCategoryListToCombotree();
		return ca;
	}

	@Override
	public List<Category> findChildrenCategoryListById(long id) {
		return this.categoryDao.findChildrenCategoryListById(id);
	}

	@Override
	public List<Category> findCategoryParentList() {
		return this.categoryDao.findCategoryParentList();
	}

}
